# /**
# * Copyright (c) 2022 Beijing Jiaotong University
# * PhotLab is licensed under [Open Source License].
# * You can use this software according to the terms and conditions of the [Open Source License].
# *
# * You may obtain a copy of [Open Source License] at: [https://open.source.license/]
# *
# * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# *
# * See the [Open Source License] for more details.
# */
# /**
# * Author: Shihao Xie
# * Created: Aug. 31, 2022
# * Supported by: National Key Research and Development Program of China
# */
import toml
import os
import platform

"""
全局变量
"""

global TEMP_ROOT

__dir__ = os.path.dirname(os.path.abspath(__file__))
__config_file_path = "../../phot_server_config.toml"
__absolute_config_file_path = os.path.abspath(os.path.join(__dir__, __config_file_path))
__abs_config_dir_path = os.path.dirname(__absolute_config_file_path)

BASE_ROOT = __abs_config_dir_path

with open(__absolute_config_file_path, "r", encoding="utf-8") as file:
    __config = toml.load(file)

IP = __config["PhotServer"]["ip"]
PORT = __config["PhotServer"]["port"]
PROTOCOL = __config["PhotServer"]["protocol"]
# if platform.system() == "Windows":
#     SERVER_TYPE = "Local"
# else:
#     SERVER_TYPE = "Remote"
SERVER_TYPE = __config["PhotServer"]["server_type"]
SERVER_VERSION = __config["PhotServer"]["server_version"]

LIBRARY_ROOT = os.path.abspath(os.path.join(__abs_config_dir_path, __config["PhotServer"]["Library"]["root"]))
# if os.path.isabs(__config["PhotServer"]["Project"]["root"]):
PROJECT_ROOT = __config["PhotServer"]["Project"]["root"]

MATERIAL_ROOT = __config["PhotServer"]["Material"]["root"]
# else:
#     PROJECT_ROOT = os.path.abspath(os.path.join(__abs_config_dir_path, __config["PhotServer"]["Project"]["root"]))
LOGGER_CONFIG_ROOT = os.path.abspath(
    os.path.join(__abs_config_dir_path, __config["PhotServer"]["Logger"]["config_root"]))
LOG_FILE_ROOT = os.path.abspath(
    os.path.join(__abs_config_dir_path, __config["PhotServer"]["Logger"]["log_file_root"]))

GRAPH_POSTFIX = __config["PhotServer"]["Postfix"]["graph"]
COMPONENT_POSTFIX = __config["PhotServer"]["Postfix"]["component"]
GRID_POSTFIX = __config["PhotServer"]["Postfix"]["grid"]
CONFIG_POSTFIX = __config["PhotServer"]["Postfix"]["project_config"]
CONFIG_FILE_NAME = "config" + CONFIG_POSTFIX
FIGURE_POSTFIX = __config["PhotServer"]["Postfix"]["figure"]

ICON_ROOT = os.path.abspath(os.path.join(__abs_config_dir_path, __config["PhotServer"]["Icon"]["root"]))

# if os.path.isabs(__config["PhotServer"]["Temp"]["root"]):
TEMP_ROOT = __config["PhotServer"]["Temp"]["root"]
# else:
#     TEMP_ROOT = os.path.abspath(os.path.join(__abs_config_dir_path, __config["PhotServer"]["Temp"]["root"]))


global_config_dict = globals()


def set_root_config(name, path):
    if os.path.exists(path) is False:
        os.makedirs(path, 0o777)
    globals()[name] = path
    if name.endswith("_ROOT"):
        pass


def roots_rel_to_abs():
    for key, value in globals().items():
        if key.endswith("_ROOT"):
            if os.path.isabs(value) is False:
                globals()[key] = os.path.abspath(os.path.join(__abs_config_dir_path, value))
                if os.path.exists(globals()[key]) is False:
                    os.makedirs(globals()[key], 0o777)


roots_rel_to_abs()

if __name__ == "__main__":
    print(global_config_dict)
    roots_rel_to_abs()
    print(global_config_dict)
